﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

using Mvc.XForms.Controls.Formatters;
using Mvc.XForms.Utilities;

namespace Mvc.XForms.Controls {
	public abstract class XFormControlBase : HtmlControlBase, IXFormControl {
		public string ClientID { get; set; }
		public string FullID { get; set; }
		public string Label { get; set; }
		public bool Relevant { get; set; }
		public IList<IAction> Actions { get; set; }
		public Expression Bind { get; set; }

		public abstract XFormControlType ControlType { get; }
		public abstract object ToJson();

		public XFormControlBase(IContainerControl parent, string id, Expression bindExp) {
			CssClass = new CssClass("xf");
			Actions = new List<IAction>();
			Attributes = new Dictionary<string, string>();
			Relevant = true;
			Bind = bindExp;

			if (parent != null) {
				ID = id ?? bindExp.ExtractID();

				if (parent.FullID != null) {
					FullID = string.IsNullOrEmpty(ID) ? parent.FullID : string.Concat(parent.FullID, ExpressionHelper.Separator, ID);
				} else {
					FullID = ID;
				}
			}
		}

		public virtual void SetLabel() {
			Label = Label ?? Bind.ExtractMember().CapitalizeSpace();
		}
	}
}
